<?php
/*
 * @Author: your name
 * @Date: 2019-12-25 10:01:52
 * @LastEditTime : 2020-01-21 15:00:41
 * @LastEditors  : Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /ahsmp_3/application/timeexecution/service/CallClient/ResentMessage.php
 */
namespace app\timeexecution\service\callclient;
use \app\timeexecution\service\Base;
class ResendMessage extends Base
{
    private $fileName = 'messageResend.lock';//文件名
    private $path = '../runtime/timexec/';//定时文件存放路径
    /**
     * @name: 发送
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-25 10:09:47
     */
    function send ()
    {
        $this->wirteLock($this->path, $this->fileName);

        $Event = $this->selectEvent();
        foreach ($Event as $v)
        {
            $SocketClient = new \app\asmpAsync\socket\SocketClient($v['ip'], $v['port']);
            $SocketClient->sendMsg(base64_decode($v['content']));
            $resendData = [
                'resend_time' => time(),
                'resend_status' => 101
            ];
            
            if ($SocketClient->sendStatus)
            {
                $resendData['resend_status'] = 200;
                $resendData['err_id'] = 0;
            }

            Model('ServerSocketMessageHistory')->where('id', $v['id'])->update($resendData);
        }

        $this->delLock($this->path . $this->fileName);
    }

    /**
     * @name: 查询事件
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-25 10:40:02
     */
    function selectEvent ()
    {
        $return = db('ServerSocketMessageHistory')->field('id,content,ip,port')->where('err_id', '>', 0)->where('resend_status', '<', 200)->order('id', 'ASC')->select();

        return $return;
    }
}
